﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MODULE_KETOAN
{
    public partial class frm_rpt_tonghopvachitietcongno : Form
    {
        ketoanDataContext context = cls_ketoan.getcontext();
        public frm_rpt_tonghopvachitietcongno()
        {
            InitializeComponent();
        }

        private void frm_rpt_tonghopvachitietcongno_Load(object sender, EventArgs e)
        {
        

            this.rdbtonghop.Checked = true;
            this.txtmachuhang.ReadOnly = true;

            }

        private void btPrint_Click(object sender, EventArgs e)
        {
            try

            {
                string tungay, denngay;
                string d, m, y;
                
                if (this.dtptungay.Value.Day >= 10)
                {
                    d = this.dtptungay.Value.Day.ToString();
                }
                else
                {
                    d = "0" + this.dtptungay.Value.Day.ToString();
                }

                if (this.dtptungay.Value.Month >= 10)
                {
                    m = this.dtptungay.Value.Month.ToString();
                }
                else
                {
                    m = "0" + this.dtptungay.Value.Month.ToString();
                }
                y = this.dtptungay.Value.Year.ToString();
                tungay = d + "/" + m + "/" + y;
                if (this.dtpdenngay.Value.Day >= 10)
                {
                    d = this.dtpdenngay.Value.Day.ToString();
                }
                else
                {
                    d = "0" + this.dtpdenngay.Value.Day.ToString();
                }

                if (this.dtpdenngay.Value.Month >= 10)
                {
                    m = this.dtpdenngay.Value.Month.ToString();
                }
                else
                {
                    m = "0" + this.dtpdenngay.Value.Month.ToString();
                }
                y = this.dtpdenngay.Value.Year.ToString();
                denngay = d + "/" + m + "/" + y;
                if (rdbtonghop.Checked == true)
                {
                    //IEnumerable<KT_rpt_bangtonghopcongnothumuaResult> lst = context.KT_rpt_bangtonghopcongnothumua(tungay, denngay).AsQueryable();
                    string connectstring = context.Connection.ConnectionString;
                    SqlConnection connection = new SqlConnection(connectstring);
                    SqlCommand command = new SqlCommand();
                    command.Connection = connection;
                    command.CommandText = "KT_rpt_bangtonghopcongnothumua'" + tungay + "','" + denngay + "'";

                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    DataTable tb = new DataTable();
                    adapter.Fill(tb);


                    MODULE_TIENICH.cls_report objReport = new MODULE_TIENICH.cls_report();


                    Cursor.Current = Cursors.WaitCursor;
                    objReport.Load_Report(Application.StartupPath + "\\reports\\bangtonghopcongnothumua.rpt", tb);
                    objReport.Formulas_Report("tungay","'"+tungay+"'");
                    objReport.Formulas_Report("denngay","'"+denngay+"'");

                    //objReport.Load_SubReport("Sub_BienBanNhap_VTNPL_BTP", objCtrl.LoadPrint_BienBanNhap_L)
                    objReport.Open_Report();
                    //objCtrl = Nothing
                    //objReport = Nothing
                    Cursor.Current = Cursors.Default;
                }
                if (rdbchitiet.Checked == true)
                {
                    string machuhang = txtmachuhang.Text;
                    string connectstring = context.Connection.ConnectionString;
                    SqlConnection connection = new SqlConnection(connectstring);
                    SqlCommand command = new SqlCommand();
                    command.Connection = connection;
                    command.CommandText = "KT_rpt_sochitietcongnothumua'"+ machuhang+"','"+ tungay + "','" + denngay + "'";

                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    DataTable tb = new DataTable();
                    adapter.Fill(tb);


                    MODULE_TIENICH.cls_report objReport = new MODULE_TIENICH.cls_report();


                    Cursor.Current = Cursors.WaitCursor;
                    objReport.Load_Report(Application.StartupPath + "\\reports\\sochitietcongnothumua.rpt", tb);
                    objReport.Formulas_Report("doituong", "'" + machuhang + "'");
                    objReport.Formulas_Report("tungay", "'" + tungay + "'");
                    objReport.Formulas_Report("denngay", "'" + denngay + "'");

                    //objReport.Load_SubReport("Sub_BienBanNhap_VTNPL_BTP", objCtrl.LoadPrint_BienBanNhap_L)
                    objReport.Open_Report();
                    //objCtrl = Nothing
                    //objReport = Nothing
                    Cursor.Current = Cursors.Default;

                }
 
            }
            catch( Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btExcel_Click(object sender, EventArgs e)
        {

            this.saveFileDialog1.Filter = "Excel file(*.xls)|*.xls";
            try
            {
                string tungay, denngay;
                string d, m, y;

                if (this.dtptungay.Value.Day >= 10)
                {
                    d = this.dtptungay.Value.Day.ToString();
                }
                else
                {
                    d = "0" + this.dtptungay.Value.Day.ToString();
                }

                if (this.dtptungay.Value.Month >= 10)
                {
                    m = this.dtptungay.Value.Month.ToString();
                }
                else
                {
                    m = "0" + this.dtptungay.Value.Month.ToString();
                }
                y = this.dtptungay.Value.Year.ToString();
                tungay = d + "/" + m + "/" + y;
                if (this.dtpdenngay.Value.Day >= 10)
                {
                    d = this.dtpdenngay.Value.Day.ToString();
                }
                else
                {
                    d = "0" + this.dtpdenngay.Value.Day.ToString();
                }

                if (this.dtpdenngay.Value.Month >= 10)
                {
                    m = this.dtpdenngay.Value.Month.ToString();
                }
                else
                {
                    m = "0" + this.dtpdenngay.Value.Month.ToString();
                }
                y = this.dtpdenngay.Value.Year.ToString();
                denngay = d + "/" + m + "/" + y;
                if (rdbtonghop.Checked == true)
                {

                    this.saveFileDialog1.FileName = "bangtonghopcongnothumua.xls";
                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        Cursor.Current = Cursors.WaitCursor;
                        List<KT_rpt_bangtonghopcongnothumuaResult> lst = context.KT_rpt_bangtonghopcongnothumua(tungay, denngay).ToList();
                        grtonghop.Rows.Clear();

                        if (lst != null)
                        {

                            for (int i = 0; i < lst.Count(); i++)
                            {
                                KT_rpt_bangtonghopcongnothumuaResult ele = lst.ElementAt(i);
                                grtonghop.Rows.Add();
                                grtonghop.Rows[i].Cells["ma"].Value = ele.ma;
                                grtonghop.Rows[i].Cells["tenkhachhang"].Value = ele.tenkhachhang;
                                grtonghop.Rows[i].Cells["thumuadauky"].Value = ele.thumuadauky;
                                grtonghop.Rows[i].Cells["thanhtoandauky"].Value = ele.thanhtoandauky;
                                grtonghop.Rows[i].Cells["thumuaphatsinh"].Value = ele.thumuaphatsinh;
                                grtonghop.Rows[i].Cells["thanhtoanphatsinh"].Value = ele.thumuaphatsinh;
                                grtonghop.Rows[i].Cells["soducuoiky"].Value = ele.soducuoiky;


                            }
                        }
                        String filePath = saveFileDialog1.FileName;

                        ExcelWrite myExcel = new ExcelWrite();
                        List<String> list = myExcel.DGVToExcel(grtonghop);
                        //String filePath = path + "\\BangKeThanhToan.xls";

                        myExcel.WriteDateToExcel(filePath, list, "A1", "E1", "A2");
                        MessageBox.Show("Export dữ liệu thành công ...", "Thông báo");
                        Cursor.Current= Cursors.Default;
                    }
                  
                }
                if (rdbchitiet.Checked == true)
                {
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void rdbtonghop_CheckedChanged(object sender, EventArgs e)
        {
          
            
        }

        private void rdbchitiet_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rdbchitiet.Checked == true)
            {
                this.txtmachuhang.ReadOnly = false;
                this.txtmachuhang.Text = "Enter để tìm";
            }
            else
            {
                this.txtmachuhang.ReadOnly = true;
                this.txtmachuhang.Text = "";
                this.txttenchuhang.Text = "";
                this.txtdiachi.Text = "";
            }

           
        }

        private void txtmachuhang_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {

                if (e.KeyCode == Keys.Enter)
                {
                    string machuhang = this.txtmachuhang.Text;
                    if (machuhang != "")
                    {
                        IEnumerable<DM_chuhang> chuhangs = from c in context.DM_chuhangs where c.machuhang.Equals(machuhang) select c;
                        if (chuhangs.Count() > 0)
                        {
                            DM_chuhang chuhang = chuhangs.ElementAt(0);
                            this.txttenchuhang.Text = chuhang.tenchuhang;
                            this.txtdiachi.Text = chuhang.diachi;

                        }
                        else
                        {
                            MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
                            cls.ShowForm("frm_timkiemchuhang");

                            this.txtmachuhang.Text = cls._makhachhang;
                            chuhangs = from c in context.DM_chuhangs where c.machuhang.Equals(this.txtmachuhang.Text) select c;
                            if (chuhangs.Count() > 0)
                            {
                                DM_chuhang chuhang = chuhangs.ElementAt(0);
                                this.txttenchuhang.Text = chuhang.tenchuhang;
                                this.txtdiachi.Text = chuhang.diachi;
                            }
                        }
                    }
                    else
                    {

                        MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
                        cls.ShowForm("frm_timkiemchuhang");

                        this.txtmachuhang.Text = cls._makhachhang;
                        IEnumerable<DM_chuhang> chuhangs = from c in context.DM_chuhangs where c.machuhang.Equals(this.txtmachuhang.Text) select c;
                        if (chuhangs.Count() > 0)
                        {
                            DM_chuhang chuhang = chuhangs.ElementAt(0);
                            this.txttenchuhang.Text = chuhang.tenchuhang;
                            this.txtdiachi.Text = chuhang.diachi;
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
